package com.bdqn.service.Impl;

import com.bdqn.dao.CheckWorkDao;
import com.bdqn.dao.Impl.CheckWorkDaoImpl;
import com.bdqn.entity.Employee;
import com.bdqn.entity.SpecialWay;
import com.bdqn.service.CheckWorkService;
import com.bdqn.util.CalenUtil;
import java.util.Date;


public class CheckWorkServiceImpl implements CheckWorkService {
    private CheckWorkDao dao = new CheckWorkDaoImpl();

    @Override
    public boolean insert(String empId, String date, String time, Employee emp,Date dd) {
        //int specialCount = dao.findSpecial(date);
        SpecialWay special = dao.findByDate(date,emp.getDepId());
        Date dates = null;
        boolean flag ;
        if(special!=null){
            //今天是特殊日期，直接添加
            if(special.getStatus() == 0){
                int count = dao.findByIdAndDate(empId,date);
                if(count <=0){
                    flag = dao.insertIntostart(time, date, empId);
                    return flag;
                }else{
                    flag=dao.insertIntoEnd(time, date, empId);
                    return flag;
                }
            }else{
                int count = dao.findByIdAndDate(empId,date);
                if(count >0 ){
                    flag = dao.insertIntoEnd(time, date, empId);
                    Date end = special.getEnd();
                    int dHours = CalenUtil.getHours(dd);
                    int eHours = CalenUtil.getHours(end);
                    int eMin = CalenUtil.getMin(end);
                    int dMin = CalenUtil.getMin(dd);
                    if( dHours< eHours ){
                        dao.updateleaveEarly(empId,date);
                    }else if(dHours==eHours && eMin<=dMin  ){
                        dao.updateleaveEarly(empId,date);
                    }
                    return flag;
                }else{
                    flag = dao.insertIntostart(time, date, empId);
                    //dates = dao.FindByIdAndDate(empId,date);
                    Date start = special.getStart();
                    int dHours = CalenUtil.getHours(dd);
                    int sHours = CalenUtil.getHours(start);
                    int dMin = CalenUtil.getMin(dd);
                    int sMin = CalenUtil.getMin(start);
                    if( dHours> sHours){
                        dao.updatelatecome(empId,date);
                    }else if(dHours==sHours && sMin<=dMin){
                        dao.updatelatecome(empId,date);
                    }
                    return flag;
                }
            }
        }else{
            //不是特殊日期
            int week = CalenUtil.getWeek(dd);
            //int weekCount = dao.findWeek(emp.getDepId(),week);
            int weekCount = dao.FindWeek(emp.getDepId(),week);
            if(weekCount == 0){
                //今天不是工作日，直接记录
                int count = dao.findByIdAndDate(empId,date);
                if(count <=0){
                    flag = dao.insertIntostart(time, date, empId);
                    return flag;
                }else{
                    flag=dao.insertIntoEnd(time, date, empId);
                    return flag;
                }
            }else{
                int count = dao.findByIdAndDate(empId,date);
                if(count >0 ){
                    flag = dao.insertIntoEnd(time, date, empId);
                    dates = dao.FindEndByIdAndDate(empId,date);
                    //dates = dao.findEnd(emp.getDepId(),week);
                    //Date end = dao.findway(week,emp.getDepId());
                    Date end = dao.findEnd(emp.getDepId(),week);
                    int dHours = CalenUtil.getHours(dates);
                    int eHours = CalenUtil.getHours(end);
                    int eMin = CalenUtil.getMin(end);
                    int dMin = CalenUtil.getMin(dates);
                    if( dHours< eHours ){
                        dao.updateleaveEarly(empId,date);
                    }else if(dHours==eHours && eMin>=dMin  ){
                        dao.updateleaveEarly(empId,date);
                    }else {
                        dao.updateleaveEarly_true(empId,date);
                    }
                    return flag;
                }else{
                    flag = dao.insertIntostart(time, date, empId);
                    dates = dao.FindByIdAndDate(empId,date);
                    //Date start = dao.findSway(week,emp.getDepId());
                    Date start = dao.findstart(emp.getDepId(),week);
                    int dHours = CalenUtil.getHours(dates);
                    int sHours = CalenUtil.getHours(start);
                    int dMin = CalenUtil.getMin(dates);
                    int sMin = CalenUtil.getMin(start);
                    if( dHours> sHours){
                        dao.updatelatecome(empId,date);
                    }else if(dHours==sHours && sMin<=dMin){
                        dao.updatelatecome(empId,date);
                    }
                    return flag;
                }
            }
        }
    }

    @Override
    public int findCout(String date, String empId) {
        return dao.findByIdAndDate(date,empId);
    }

    @Override
    public boolean delete(String empId,String name) {
        int count = dao.findByEmpIdAndName(empId,name);
        if(count >0){
            dao.deleteFromCheck(empId);
            return dao.deleteByEmpId(empId)>0;
        }else {
            return false;
        }
    }

    @Override
    public boolean addEmp(String empId, String name, String depId, String password) {
        if(dao.findByEmpId(empId)>0){
            return false;
        }else {
            return dao.addEmpl(empId,name,depId,password) >0;
        }
    }
}
